//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "fn_bleeding.sqf"
#include <btc_macros.h>
TRACE_1("Start",_this);


	_unit = _this select 0;
	BTC_r_bleeding_loop = true;
	BTC_r_unc = false;BTC_r_unc_loop = false;
	//titleText ["", "BLACK IN"];
	_timeout_bleed = time;
	_timeout_eff_mor = 0;
	_timeout_eff = time + 10;
	_timeout_mor   = 0;
	_unit setVariable ["BTC_r_status",[1,(_unit getVariable "BTC_r_status" select 1),(_unit getVariable "BTC_r_status" select 2),(_unit getVariable "BTC_r_status" select 3),(_unit getVariable "BTC_r_status" select 4)],true];
	hint "You're bleeding!";
	while {BTC_r_bleeding_loop} do
	{
		sleep 0.1;
		if (((_unit getVariable "BTC_r_status" select 0)) == 0 && ((_unit getVariable "BTC_r_status" select 1)) == 0 && (_unit getVariable "BTC_r_status" select 2) == 0) then {BTC_r_bleeding_loop = false;};
		//Bleeding
		if (((_unit getVariable "BTC_r_status" select 0)) == 1 && time > _timeout_bleed) then 
		{
			private ["_moving_ratio"];
			_moving_ratio = 1;
			if (speed player > 1 && speed player < 3) then {_moving_ratio = 1.1;};
			if (speed player >= 3 && speed player < 14) then {_moving_ratio = 1.2;};
			if (speed player >= 14) then {_moving_ratio = 1.3;};
			_unit setVariable ["BTC_r_status",[(_unit getVariable "BTC_r_status" select 0),((_unit getVariable "BTC_r_status") select 1) + (((BTC_r_damage) + (BTC_r_damage_legs) + (BTC_r_damage_hands)) * _moving_ratio),(_unit getVariable "BTC_r_status" select 2),(_unit getVariable "BTC_r_status" select 3),(_unit getVariable "BTC_r_status" select 4)],true];
			_timeout_bleed = _timeout_bleed + 2;
			//SPEED
			// > 1 < 3 striscia
			// < 14 crouch
			// >= 14 corre
			//player sidechat format ["Bleeding: %1",((_unit getVariable "BTC_r_status") select 1)];
		};
		//modifica con appannamento
		if (time > _timeout_eff) then
		{
			private ["_n"];
			//if (((_unit getVariable "BTC_r_status") select 1) > 50 || ((_unit getVariable "BTC_r_status") select 0) == 1 || ((_unit getVariable "BTC_r_status") select 2) == 1) then {};
			if (((_unit getVariable "BTC_r_status") select 1) > 10) then
			{
				_n = 5 + ((100 - ((_unit getVariable "BTC_r_status") select 1))/10);
				//player sideChat "APP" + str(_n);
				titleText ["", "BLACK OUT",_n];
			};
			_timeout_eff = time + 7;
		};
		if (((_unit getVariable "BTC_r_status") select 1) == 0) then {titleText ["", "PLAIN"];};
		if (BTC_r_damage_legs > 0.45 || ((_unit getVariable "BTC_r_status") select 1) > 55) then {player forceWalk true;} else {player forceWalk false;};
		//morphine
		if ((_unit getVariable "BTC_r_status" select 2) == 1 && _timeout_mor == 0) then {_timeout_mor = time + 120;_timeout_eff_mor = time + 3;}; //else {_timeout_mor = 0;_timeout_eff_mor = 0;};
		if ((_unit getVariable "BTC_r_status" select 2) == 1 && time > _timeout_eff_mor) then {addCamShake [2,4,3];_timeout_eff_mor = time + 3;};
		//if ((BTC_r_damage > 1) && ((_unit getVariable "BTC_r_status" select 3) != 1)) then {_unit setVariable ["BTC_r_status",[((_unit getVariable "BTC_r_status") select 0),(_unit getVariable "BTC_r_status" select 1),(_unit getVariable "BTC_r_status" select 2),1],true];_timeout_mor = time + 120;};
		//medikit - perchè in base a BTC_r_damage? Solo se in attesa di revive oppure ferito armi e gambe
		//if ((((_unit getVariable "BTC_r_status") select 0) > 75) && (_unit getVariable "BTC_r_status" select 3) != 1) then {_unit setVariable ["BTC_r_status",[((_unit getVariable "BTC_r_status") select 0),(_unit getVariable "BTC_r_status" select 1),(_unit getVariable "BTC_r_status" select 2),1],true];};
		//Serve un medikit se ferito a gambe o braccia
		if (BTC_r_damage_legs > 0.50 && !BTC_r_med_effect) then
		{
			_unit setHit ["legs", 1];_unit setfatigue 1;_med = [] spawn BTCR_fnc_waitForMed;
		};
		if (BTC_r_damage_hands > 0.5 && !BTC_r_med_effect) then
		{
			_unit setHit ["hands", 1];_med = [] spawn BTCR_fnc_waitForMed;//_unit setfatigue 1;
		};
		//Passa al wait for revive o morte
		if (((_unit getVariable "BTC_r_status") select 1) >= 100) then {BTC_r_bleeding_loop = false;};
		//Può svenire se perde troppo sangue o per dolore
		if ((((_unit getVariable "BTC_r_status") select 1) > 65 || (time > _timeout_mor && _timeout_mor != 0)) && !BTC_r_unc) then 
		{
			if (((random 1.5) < (((_unit getVariable "BTC_r_status") select 1) / 100) && !BTC_r_unc) || (time > _timeout_mor)) then
			{
				if ((_unit getVariable "BTC_r_status" select 2) == 1 && !BTC_r_unc) then {_timeout_mor = time + 120;};
				_unit spawn
				{
					if (!BTC_r_unc && !BTC_r_unc_loop) then
					{
						BTC_r_unc = true;
						BTC_r_unc_loop = true;
						//ANIMATION DEAD
						//Placeholder
						if (vehicle _this == _this) then {_this switchMove "AmovPpneMstpSnonWnonDnon_relax";};
						// TYRGHEN
						//disableUserInput true;
						titleText ["", "BLACK OUT"];
						sleep 1;
						titleText ["", "BLACK FADED"];
						for [{_n = (4 + random 6)}, {_n > 0 && ((_this getVariable "BTC_r_status") select 1) < 100 && BTC_r_bleeding_loop && _this getVariable "BTC_need_revive" == 0}, {_n = _n - 0.5}] do
						{
							//hint format ["Unconscious %1",_n];
							titleText ["", "BLACK FADED"];
							sleep 0.5;
						};
						//TYRGHEN
						//disableUserInput false;
						BTC_r_unc_loop = false;
						titleText ["", "BLACK IN"];
						if (_this getVariable "BTC_need_revive" == 0) then
						{
							if (vehicle _this == _this) then {_this playMoveNow "AmovPpneMstpSnonWnonDnon_healed";};
							sleep 0.5;
							titleText ["", "PLAIN"];
							sleep 25;
						};
						BTC_r_unc = false;
					};
				};
			};
		};
	};
	BTC_r_bleeding_loop = false;
	if (((_unit getVariable "BTC_r_status") select 1) >= 100) then
	{
		if (BTC_r_wait_for_revive == 1) then
		{
			[_unit] spawn BTCR_fnc_waitForRevive;
		}
		else
		{
			_unit setDamage 1;
		};
	};
